//
//  simModifyPasswdViewController.m
//  simens
//
//  Created by xx xx on 12-8-3.
//  Copyright (c) 2012年 yst. All rights reserved.
//

#import "simModifyPasswdViewController.h"
#import "simMessageViewController.h"
#import "ASIHTTPRequest.h"

@interface simModifyPasswdViewController ()<UITextFieldDelegate,MBProgressHUDDelegate>

-(void) missKeyBoard;

@property (nonatomic,strong) UITextField* oldPasswd;
@property (nonatomic,strong) UITextField* newpd;
@property (nonatomic,strong) UITextField* confirmPasswd;
@property (nonatomic,strong) UIButton* submitButton;
@property (nonatomic,strong) UIButton* cancelButton;
@property (nonatomic,strong) UITableView *passwdTableView;

@end



@implementation simModifyPasswdViewController

@synthesize oldPasswd=_oldPasswd;
@synthesize newpd=_newpd;
@synthesize confirmPasswd = _confirmPasswd;
@synthesize submitButton = _submitButton;
@synthesize cancelButton = _cancelButton;
@synthesize passwdTableView = _passwdTableView;

ASIHTTPRequest* submitRequest;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    
    
    self.passwdTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width,
                                                                       self.view.bounds.size.height)
                                                      style:UITableViewStyleGrouped];
    
    [self.passwdTableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.view addSubview: self.passwdTableView];
    
    
    
    self.title=@"密码修改";
    [self.view setBackgroundColor:[UIColor clearColor]];
    self.oldPasswd = [[UITextField alloc] initWithFrame:CGRectMake(45, 40.0, 230, 35)];
    self.oldPasswd.borderStyle = UITextBorderStyleBezel;
    self.oldPasswd.textColor = [UIColor blackColor];
    self.oldPasswd.font = [UIFont systemFontOfSize:20.0];
    self.oldPasswd.placeholder = @"请输入旧密码";
    self.oldPasswd.backgroundColor = [UIColor whiteColor];
    [self.oldPasswd setDelegate:self];
    self.oldPasswd.keyboardType = UIKeyboardTypeDefault;
    self.oldPasswd.returnKeyType = UIReturnKeyDefault;
    self.oldPasswd.secureTextEntry = YES;	// make the text entry secure (bullets)
    self.oldPasswd.clearButtonMode = UITextFieldViewModeWhileEditing;	// has a clear 'x' button to the right
    //self.oldPasswd.delegate = self;	// let us be the delegate so we know when the keyboard's "Done" button is pressed
    // Add an accessibility label that describes what the text field is for.
    [self.oldPasswd setAccessibilityLabel:NSLocalizedString(@"SecureTextField", @"")];
    [self.view addSubview:self.oldPasswd];
    
    
    self.newpd = [[UITextField alloc] initWithFrame:CGRectMake(45, 100.0, 230, 35)];
    self.newpd.borderStyle = UITextBorderStyleBezel;
    self.newpd.textColor = [UIColor blackColor];
    self.newpd.font = [UIFont systemFontOfSize:20.0];
    self.newpd.placeholder = @"请输入新密码";
    self.newpd.backgroundColor = [UIColor whiteColor];
    [self.newpd setDelegate:self];
    self.newpd.keyboardType = UIKeyboardTypeDefault;
    self.newpd.returnKeyType = UIReturnKeyDefault;
    self.newpd.secureTextEntry = YES;	// make the text entry secure (bullets)
    self.newpd.clearButtonMode = UITextFieldViewModeWhileEditing;	// has a clear 'x' button to
    //self.newpd.delegate = self;	// let us be the delegate so we know when the keyboard's "Done" button is pressed
    // Add an accessibility label that describes what the text field is for.
    [self.newpd setAccessibilityLabel:NSLocalizedString(@"SecureTextField", @"")];
    [self.view addSubview:self.newpd];

    
    self.confirmPasswd = [[UITextField alloc] initWithFrame:CGRectMake(45, 160.0, 230, 35)];
    self.confirmPasswd.borderStyle = UITextBorderStyleBezel;
    self.confirmPasswd.textColor = [UIColor blackColor];
    self.confirmPasswd.font = [UIFont systemFontOfSize:20.0];
    self.confirmPasswd.placeholder = @"请输入确认新密码";
    self.confirmPasswd.backgroundColor = [UIColor whiteColor];
    [self.confirmPasswd setDelegate:self];
    self.confirmPasswd.keyboardType = UIKeyboardTypeDefault;
    self.confirmPasswd.returnKeyType = UIReturnKeyDefault;
    self.confirmPasswd.secureTextEntry = YES;	// make the text entry secure (bullets)
    self.confirmPasswd.clearButtonMode = UITextFieldViewModeWhileEditing;	// has a clear 'x' button to the right
    //self.confirmPasswd.delegate = self;	// let us be the delegate so we know when the keyboard's "Done" button is pressed
    // Add an accessibility label that describes what the text field is for.
    [self.confirmPasswd setAccessibilityLabel:NSLocalizedString(@"SecureTextField", @"")];
    [self.view addSubview:self.confirmPasswd];

    
    
    self.submitButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.submitButton setFrame:CGRectMake(90,240,150,40)];
    [self.submitButton setTitle:@"确认" forState:(UIControlStateNormal)];
    //定义点击时的响应函数
    [self.submitButton addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview: self.submitButton];
    
//    self.cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//    [self.cancelButton setFrame:CGRectMake(170,280,120,50)];
//    [self.cancelButton setTitle:@"取消" forState:(UIControlStateNormal)];
//    //定义点击时的响应函数
//    [self.cancelButton addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview: self.cancelButton];
    
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (void)cancel{
    
    simMessageViewController* messageViewController = [[simMessageViewController alloc] init];
    
    [self.navigationController pushViewController:messageViewController animated:YES];
    
    return;
}

- (void)submit{
    [self missKeyBoard];
    if ([self.oldPasswd.text isEqualToString:@""] || self.oldPasswd.text == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"旧密码为空" message:@"请输入旧密码"
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        
        return;
    }
    
    if ([self.newpd.text isEqualToString:@""] || self.newpd.text == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"消息" message:@"新密码为空,请输入新密码"
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        
        return;
    }
    
    if ([self.confirmPasswd.text isEqualToString:@""] || self.confirmPasswd.text == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"消息" message:@"确认密码为空，请输入确认密码"
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        return;
    }
    
    if ([self.oldPasswd.text isEqualToString: self.newpd.text]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"消息" message:@"新密码不能为旧密码，请重新输入"
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        return;
    }
    
    if ([self.newpd.text isEqualToString: self.confirmPasswd.text]) {
        [self showMbprogress];
        
        NSString *u =[NSString stringWithFormat:@"%@/updateSiemensEmp?mobile=%@&pwd=%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"WebserviceUrl"],[[NSUserDefaults standardUserDefaults]objectForKey:@"LoginMobile"],[[NSUserDefaults standardUserDefaults]objectForKey:@"LoginPassword"]];
        
        NSMutableString *ms = [[NSMutableString alloc] init];
        [ms appendString:u];
        [ms appendString:@"&newPwd="];
        [ms appendString:self.newpd.text];
        
        
        
        NSURL *url = [NSURL URLWithString: ms];
        
        submitRequest = [ASIHTTPRequest requestWithURL:url];
        [submitRequest setDelegate:self];
        [submitRequest start];
        
        
        
        
        
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"新密码" message:@"新密码与确认密码不一致，请重新设置"
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show]; 
        return;
    }
    
}

/*
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    [self hideMbProgress];
    
    simMessageViewController* messageViewController = [[simMessageViewController alloc] init];
    
    [self.navigationController pushViewController:messageViewController animated:YES];
    
}
 */


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void) missKeyBoard {
    
    [self.oldPasswd resignFirstResponder];
    [self.newpd resignFirstResponder];
    [self.confirmPasswd resignFirstResponder];
}


- (void)requestFinished:(ASIHTTPRequest *)request {
    NSError *error = [request error];
    if(request==submitRequest){
        [self showMbprogress];
        if (!error) {
            NSString *response = [request responseString];
            if([response isEqualToString:@"true"]){
                
                [[NSUserDefaults standardUserDefaults] setObject:self.confirmPasswd.text forKey:@"LoginPassword"];

                
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"消息" message:@"密码修改成功"
                                                               delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
                [alert show];
                
                [self hideMbProgress];
                    
                simMessageViewController* messageViewController = [[simMessageViewController alloc] init];
                    
                [self.navigationController pushViewController:messageViewController animated:YES];
                
                

            }else{
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"消息" message:@"密码修改失败"
                                                               delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
                [alert show];
                [self hideMbProgress];
            }
        }
        
    }
}


- (void)requestFailed:(ASIHTTPRequest *)request {
    [self hideMbProgress];
}

//iPad键盘的Retuan...电脑的回车键


- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if(textField == self.newpd){
    
        [self.newpd resignFirstResponder];
    }else if (textField == self.oldPasswd) {
        
        //用户名输入完成，点击键盘Return
        
        [self.oldPasswd resignFirstResponder];
        
    } else if (textField == self.confirmPasswd) {
        //启动登陆
        //[self submit];
        [self.confirmPasswd resignFirstResponder];
    }
    return YES;
}

@end
